package com.sunday.player;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import com.sunday.player.domain.musiclist;
import com.sunday.player.service.PlayerService;
import com.sunday.player.service.musicplayerCallBack;
import com.sunday.player.service.musicservicecontrol;
import com.sunday.player.util.AlertBoxColtroler;
import com.sunday.player.util.SundaySharedPreferences;
import com.sunday.player.util.listDBcontroler;
import com.sunday.player.util.playerControl;

public class basicActivity extends Activity {
    public final String TAG = this.getClass().getSimpleName();
    public musicservicecontrol PlayerControler;
    public SundaySharedPreferences sundaySharedPreferences;
    public ProgressDialog progressDialog ;
    private listDBcontroler dBcontroler;
    public Handler defaultHandler = new Handler();
    public AlertBoxColtroler alertColtroler ;
    private long clickTime = 0;
    private IntentFilter filter;

    public musicplayerCallBack playerCallBack = new musicplayerCallBack.Stub() {

	@Override
	public void ReceivePlayerState(int state) throws RemoteException {
	    playerControl.trace("i", "ReceivePlayerState = " + state);
	    recivePlayerState(state);
	}
        
      
    };
      
    
    private final ServiceConnection playControlConnection = new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            try {
		PlayerControler.setupCallback(playerCallBack);
	    } catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            PlayerControler = musicservicecontrol.Stub.asInterface(service);
            try {
		PlayerControler.setupCallback(playerCallBack);
	    } catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
            playerControl.trace("i", "onServiceConnected");
            basicActivity.this.onServiceConnected();
            
            
            
           
        }
    };
    /**
     * AIDL 서비스 호출용 
     */
    public void onServiceConnected(){}
    /**
     * 플레이어 상태 전달
     * 음악이 바뀌거나 .
     * @param state
     */
    protected void recivePlayerState(int state) {}
   
    /**
     * 리소스 스트링값 반환
     * @param ID
     * @return
     */
    public String getResourseString(int ID){
	return getResources().getString(ID);
    }
    @Override
    protected void onStart() {
	playerControl.trace("i", "onStart"+this.getClass().getSimpleName());
	super.onStart();
	//getApplicationContext().bindService(new Intent(getApplicationContext() , PlayerService.class), playControlConnection, Context.BIND_AUTO_CREATE);
	
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	initProgressDilog();
	sundaySharedPreferences = new SundaySharedPreferences(this);
	alertColtroler = new AlertBoxColtroler(this);
	this.dBcontroler = new listDBcontroler(this);
	Intent sv = new Intent(playerControl.ServiceID);
	
	filter = new IntentFilter(playerControl.BR_ACTION);
	
	
	getApplicationContext().startService(sv);
	getApplicationContext().bindService(new Intent(getApplicationContext() , PlayerService.class), playControlConnection, Context.BIND_AUTO_CREATE);
		
        super.onCreate(savedInstanceState);
        playerControl.trace("i", "onCreate");
    }
   
    private void initProgressDilog() {
	progressDialog  = new ProgressDialog(this);
	progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
    }
    @Override
    protected void onPause() {
	playerControl.trace("i", "onPause"+this.getClass().getSimpleName());
        // TODO Auto-generated method stub
	//getApplicationContext().unbindService(playControlConnection);
        super.onPause();
        
        //getApplicationContext().unbindService(playControlConnection);
    }
    @Override
    protected void onStop() {
	playerControl.trace("i", "onStop"+this.getClass().getSimpleName());
	
	    
	super.onStop();
    }
    @Override
    protected void onDestroy() {
	playerControl.trace("i", "onDestroy"+this.getClass().getSimpleName());
        // TODO Auto-generated method stub
	getApplicationContext().unbindService(playControlConnection);
        super.onDestroy();
        
    }
    @Override
    protected void onResume() {
	playerControl.trace("i", "onResume"+this.getClass().getSimpleName());
        // TODO Auto-generated method stub
	
        super.onResume();
    }
    public void startActivity(){
	
    }
    
    public void showProgerssBar(String textbody){
	progressDialog.setMessage(textbody);
	progressDialog.setCancelable(false);
	progressDialog.show();
    }
    public void hideProgressBar(boolean isUpdate){
	if(progressDialog.isShowing()){
	    progressDialog.dismiss();
	}
	if(isUpdate){
		onScreenUpdate();
	}
    }
    /**
     * 화면새로고침.
     * 아 ... 뭔가 획기적인 방법이 없을려나 ...
     */
    public void onScreenUpdate() {
	
    }
    public listDBcontroler getdBcontroler() {
	return dBcontroler;
    }
   /**
    * 사용되면 어떤 버튼이든지 연속으로 클릭되는것을 막는다 .
    * t 클릭가능
    * f 클릭불가
    * @return
    */
    public boolean Doublechecker(){
	if((System.currentTimeMillis() - clickTime) > 1000){
	    clickTime = System.currentTimeMillis();
	    return true;
	}else{
	    return false;
	}
    }
    
    /**
     * 플레이 리스트에 삽입
     * @param item
     * @throws RemoteException
     */
    public void Playlist_Insert(musiclist item) throws RemoteException{
	getdBcontroler().playList_insertAll(item.getMusiclist());
	PlayerControler.getMusicList().addAll(item.getMusiclist());
    }
    /**
     * 플레이 리스트를 재구성합니다 
     * 기존의 목록을 삭tothe제
     * @throws RemoteException 
     */
    public void Playlist_allget() throws RemoteException{
	
	PlayerControler.getMusicList().clear();
	PlayerControler.getMusicList().addAll(getdBcontroler().playList_get());
	Toast.makeText(this, getString(R.string.Toast_Mess_ListLoading), Toast.LENGTH_SHORT).show();
    }
}
